using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace TestCecile
{
    public partial class KlantenBrowser : Form
    {
        private string zoekStatus = "KLNM";
        private string zoekWaarde = "";
        public KlantenBrowser()
        {
            InitializeComponent();
        }

       
        private void KlantenBrowser_Load(object sender, EventArgs e)
        {
            Text = "KLANTENOVERZICHT";
            this.kLANTTableAdapter.FillZonderDeleted(this.dataSet1.KLANT);
            //this.kLANTTableAdapter.Fill(this.dataSet1.KLANT);
            kLANTBindingSource.Sort = "KLNM";
        }

        private void txtZoekKlnm_TextChanged(object sender, EventArgs e)
        {
            kLANTBindingSource.Sort = "KLNM";
            btnZoek.Text = "Zoek op klantnaam";
            zoekStatus = "KLNM";
            zoekWaarde = txtZoekKlnm.Text.Trim();
        }

        private void txtZoekHuisnr_TextChanged(object sender, EventArgs e)
        {
            kLANTBindingSource.Sort = "HUISNR";
            btnZoek.Text = "Zoek op huisnr";
            zoekStatus = "HUISNR";
            zoekWaarde = txtZoekHuisnr.Text.Trim();
        }

        private void txtZoekPostcode_TextChanged(object sender, EventArgs e)
        {
            kLANTBindingSource.Sort = "POSTNR";
            btnZoek.Text = "Zoek op postCode";
            zoekStatus = "POSTNR";
            zoekWaarde = txtZoekPostcode.Text.Trim();
        }

        private void btnZoek_Click(object sender, EventArgs e)
        {   
            int positie = 0;
            try
            {
                if (zoekStatus.Equals("KLNM"))
                    positie = kLANTBindingSource.Find("KLNM", zoekWaarde);
                if (zoekStatus.Equals("HUISNR"))
                    positie = kLANTBindingSource.Find("HUISNR", zoekWaarde);
                if (zoekStatus.Equals("POSTNR"))
                    positie = kLANTBindingSource.Find("POSTNR", zoekWaarde);
                kLANTBindingSource.Position = positie;
                kLANTDataGridView.Focus();
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
            txtZoekKlnm.Text = "";
            txtZoekHuisnr.Text = "";
            txtZoekPostcode.Text = "";
        }

        private void txtFilterKlnm_TextChanged(object sender, EventArgs e)
        {
            try
            {
            string vZoek = txtFilterKlnm.Text.Trim();
            if (vZoek.Length > 0)
            {
                if (vZoek.Substring(0, 1).Equals("'"))
                    vZoek = "'" + vZoek;
            }
            
                this.kLANTBindingSource.Filter = "KLNM like '" + vZoek + "%'";
            }
            catch (Exception) { }
          //  Text = kLANTBindingSource.Filter.ToString();
        }

        private void txtFilterKlnmDeel_TextChanged(object sender, EventArgs e)
        {
            string vZoek = txtFilterKlnmDeel.Text.Trim();
            if (vZoek.Length > 0)
            {
                if (vZoek.Substring(0, 1).Equals("'"))
                    vZoek = "'" + vZoek;
            }
            try
            {
                this.kLANTBindingSource.Filter = "KLNM like '%" + vZoek + "%'";
            }
            catch (Exception) { }
            
        }

        private void txtFilterStraat_TextChanged(object sender, EventArgs e)
        {
            string vZoek = txtFilterStraat.Text.Trim();
            if (vZoek.Length > 0)
            {
                if (vZoek.Substring(0, 1).Equals("'"))
                    vZoek = "'" + vZoek;
            }
            try
            {
                this.kLANTBindingSource.Filter = "STRAAT like '" + vZoek + "%'";
            }
            catch (Exception) { MessageBox.Show("djuuuu"); }
        }

        private void txtFilterStraatDeel_TextChanged(object sender, EventArgs e)
        {
            string vZoek = txtFilterStraatDeel.Text.Trim();
            if (vZoek.Length > 0)
            {
                if (vZoek.Substring(0, 1).Equals("'"))
                    vZoek = "'" + vZoek;
            }
            try
            {
                this.kLANTBindingSource.Filter = "STRAAT like '%" + vZoek + "%'";
            }
            catch (Exception) { }
        }

        private void btnAlleKlanten_Click(object sender, EventArgs e)
        {
            kLANTBindingSource.RemoveFilter();
            kLANTBindingSource.Sort = "KLNM";
        }

        private void btnNieuw_Click(object sender, EventArgs e)
        {
            int klantNr = -99;
            UpdateCreateForm uc = new UpdateCreateForm(klantNr, dataSet1, this);
            uc.ShowDialog();
            //this.kLANTTableAdapter.Fill(this.dataSet1.KLANT);
        }

        private void btnWijzig_Click(object sender, EventArgs e)
        {
            try
            {
                DataRowView drv = kLANTBindingSource.Current as DataRowView;
                int klantNr = int.Parse(drv["KLNR"].ToString());
               // Text = "Klnr " + klantNr;
                UpdateCreateForm uc = new UpdateCreateForm(klantNr, dataSet1, this);
                uc.ShowDialog();
                kLANTBindingSource.EndEdit();               
            }
            catch (Exception ex) {MessageBox.Show(ex.Message); }
        }

        private void btnWis_Click(object sender, EventArgs e)
        {
            DataRowView drv = kLANTBindingSource.Current as DataRowView;
            int vKlnr = int.Parse(drv["KLNR"].ToString());
            string klantInfo = drv["KLNM"].ToString().Trim() + " " +
                               drv["KLVNM"].ToString().Trim() + " (" +
                               drv["STRAAT"].ToString().Trim() + " " +
                               drv["STAD"].ToString().Trim() + ")";

            DialogResult antwoord = MessageBox.Show("DEFINITIEF wissen van " + klantInfo + " en al zijn aankopen (aankoopfiches)???", "WISSEN KLANT", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (antwoord == DialogResult.Yes)
            {
                try
                {
                    aANKOOPTableAdapter.UpdateAsDeleted(vKlnr);
                    aANKOOPTableAdapter.DeleteAankopen(vKlnr);
                    kLANTTableAdapter.UpdateAsDeleted(-1, vKlnr);
                    kLANTTableAdapter.DeleteKlant(vKlnr);
                    int pos = kLANTBindingSource.Position;
                    kLANTBindingSource.RemoveCurrent();
                    kLANTBindingSource.Position = Math.Max(1,pos - 1);
                }
                catch (Exception ex) { MessageBox.Show(ex.Message); }
                //catch (Exception ex) {MessageBox.Show(klantInfo+ "NIET gewist!!!");}
             }
        }

        private void btnAlleAankopen_Click(object sender, EventArgs e)
        {
            AankoopBrowser ab = new AankoopBrowser();
            ab.ShowDialog();
        }

        private void btnAankopenKlant_Click(object sender, EventArgs e)
        {
            DataRowView drv = kLANTBindingSource.Current as DataRowView;
            int vKlNr = int.Parse(drv["KLNR"].ToString());
            string vKlantInfo = drv["KLNM"].ToString().Trim() + " "+drv["KLVNM"].ToString().Trim() + Environment.NewLine +
                                drv["POSTNR"].ToString().Trim() + " " + drv["STAD"].ToString().Trim();
            AankoopFiche ab = new AankoopFiche(vKlNr, vKlantInfo, drv);
            ab.ShowDialog();
        }

        private void kLANTDataGridView_SelectionChanged(object sender, EventArgs e)
        {
            try
            {
                DataRowView drv = kLANTBindingSource.Current as DataRowView;
                btnAankopenKlant.Text = "Aankoop Fiche voor " + drv["KLNM"].ToString();
            }
            catch (Exception ex) {}//MessageBox.Show("foert"+ex.Message); }
        }

        public void PositioneerOpNieuweKlant(int vKlNr)
        {
            int positie = kLANTBindingSource.Find("KLNR", vKlNr);
            kLANTBindingSource.Position = positie;
        }


        private void dgvPrinten_Click(object sender, EventArgs e)
        {
            PrintenFilterForm ab = new PrintenFilterForm();
            ab.ShowDialog();
        }

        private void kLANTDataGridView_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            //Dummy
        }

                

        
    }
}